﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using G15_Agent.Helpers.ServiceHelpers;
using G15_Agent.Models.Transport;
using G15_Agent.Helpers.ServiceHelpers.TransportServiceHelpers;
using G15_Agent.Models.ViewModels.TransportViewModels;
using G15_Agent.Helpers;


namespace G15_Agent.Controllers
{
    public class TransportController : Controller
    {
        public ActionResult Index()
        {
            List<string> listService = new List<string>();
            foreach (var service in ServiceFactory.Transport)
            {
                listService.Add(service.ServiceID);
            }

            return View(listService);
        }

        public ActionResult SearchTransport(string serviceID)
        {
            bool isError = false;
            var service = (TransportService)ServiceFactory.GetService(serviceID);
            SearchTransportViewModel viewModel = new SearchTransportViewModel();

            try
            {
                viewModel.serviceID = serviceID;
                viewModel.dsMien = service.RetrieveRegionList();
                viewModel.dsHieuXe = service.RetrieveVehicleBrand();
                viewModel.dsSoCho = service.GetSeatOption();
                viewModel.dsDichVu = service.GetServiceType();
            }
            catch (Exception ex)
            {
                isError = true;
            }
            
            if (isError || serviceID == null || viewModel.dsMien == null || viewModel.dsHieuXe == null)
            {
                return View();
            }

            return View(viewModel);            
        }

        [HttpPost]
        public ActionResult SearchTransport(SearchTransportViewModel data)
        {
            //if (ModelState.IsValid)
            if (data != null)
            {
                return RedirectToAction("ListTransport", 
                    new { 
                        serviceID = data.serviceID, 
                        region = data.maMien, 
                        brand = data.maHieuXe, 
                        seat = data.soCho, 
                        serviceType = data.maDichVu
                    });
            }

            return RedirectToAction("SearchTransport", new { serviceID = data.serviceID });
        }

        public ActionResult ListTransport(string serviceID, string region, string brand, string seat, string serviceType)
        {
            bool isError = false;
            var service = (TransportService)ServiceFactory.GetService(serviceID);
            ListTransportViewModel viewModel = new ListTransportViewModel();

            try
            {
                viewModel.serviceID = serviceID;
                if (serviceID == null) serviceID = "";
                if (brand == null) brand = "";
                if (seat == null) seat = "";
                if (serviceType == null) serviceType = "";

                viewModel.dsLoaiXe = service.SearchForTransportation(region, brand, seat, serviceType);
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || serviceID == null || viewModel.dsLoaiXe == null)
            {
                return View();
            }

            return View(viewModel);
        }

        [Authorize]
        public ActionResult BookTransport(string serviceID, string vehicleID)
        {
            if (serviceID == null || vehicleID == null)
                return View();
            BookTransportViewModel viewModel = new BookTransportViewModel();
            viewModel.serviceID = serviceID;
            viewModel.vehicleID = vehicleID;
            viewModel.ngayDi = System.DateTime.Today;
            viewModel.ngayVe = System.DateTime.Today;
            viewModel.soTaiXe = 1;
            viewModel.soLuong = 1;

            bool isError = false;
            var service = (TransportService)ServiceFactory.GetService(serviceID);
            try
            {
                viewModel.loaiXe = service.RetrieveVehicleList().FirstOrDefault(m => m.id == int.Parse(vehicleID));
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || serviceID == null || viewModel.loaiXe == null)
            {
                return View();
            }

            return View(viewModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult BookTransport(BookTransportViewModel data)
        {
            if (data == null)
                return RedirectToAction("BookTransport", new { serviceID = data.serviceID, vehicleID = data.loaiXe.id.ToString()});

            int? maHoaDon = null;
            bool isError = false;
            LoaiXe loaiXe = null;
            var service = (TransportService)ServiceFactory.GetService(data.serviceID);
            try
            {
                loaiXe = service.RetrieveVehicleList().FirstOrDefault(m => m.id == int.Parse(data.vehicleID)); ;
                maHoaDon = service.BookingForTransportation(
                    loaiXe.tenXe, loaiXe.dichVu, data.ngayDi, data.ngayVe, data.soTaiXe, data.soLuong);
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || maHoaDon == null)
                return RedirectToAction("BookTransport", new { serviceID = data.serviceID, vehicleID = data.loaiXe.id.ToString() });

            string orderDetail =
                @"Hãng vận chuyển: " + DatabaseHelper.GetServiceName(data.serviceID) +
                @"\nTên xe: " + loaiXe.tenXe +
                @"\nNgày đi: " + data.ngayDi.ToShortDateString() +
                @"\nNgày về: " + data.ngayVe.ToShortDateString() +
                @"\nSố chổ: " + data.soLuong.ToString();

            DatabaseHelper.AddOrderDetail(
                data.serviceID,
                DatabaseHelper.GetUserID(User.Identity.Name),
                orderDetail,
                (int)(loaiXe.gia + loaiXe.chiPhiTaiXe));

            return View();
        }

    }
}
